let {Session} = require('../database/sequelize');
let {sessionInvalid} = require('../base/result');

function session(req, res, next) {
  let uid = req.body.uid;
  let sess_id = req.get('sess_id');

  if (!uid || !sess_id) {
    res.send({"success": false, "msg": "参数错误"});
    return;
  }


  Session.findOne({
    where: {id: sess_id}
  }).then(session => {
    if (session) {
      let data = session.dataValues;
      let userInfo = JSON.parse(data.userinfo);
      if (userInfo.id == uid && data.expired > (new Date().getTime() / 1000)) {
        next();
      } else {
        res.send(sessionInvalid());
        return
      }

    } else {
      res.send(sessionInvalid());
      return
    }

  });

}

module.exports = session;
